package dk.dashboardFP.panels.displayPanel {
	import dk.dashboardFP.contextPanel.AbstractContextPanel;
	import dk.dashboardFP.utils.DisplayChainTools;

	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.geom.Point;

	/**
	 * @author josef
	 */
	public class SelectionList extends AbstractContextPanel {
		private var _doOriginalSelection : DisplayObject;
		private var _a_doListElements : Array;
		private var _doSelectedListItem : SelectionListItem;
		private var _doDefaultSelection : SelectionListItem;

		public function SelectionList(doSelection : DisplayObject) {
			
			_doOriginalSelection = doSelection;
			super(new Point());
			
			setupElements();
			drawList();
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// setup
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function setupElements() : void {
			
			_a_doListElements = [];
			
			//:::::::
			// register events
			addEventListener(SelectionListItemEvent.OVER_ITEM, onOverItem, false, 0, true); 
			addEventListener(SelectionListItemEvent.OUT_ITEM, onOutItem, false, 0, true); 
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// selection
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function onOutItem(event : SelectionListItemEvent) : void {
			markItem(_doDefaultSelection);
		}

		private function onOverItem(event : SelectionListItemEvent) : void {
			markItem((event.target as SelectionListButton).parent as SelectionListItem);
		}

		private function markItem(doNewSelectionListItem : SelectionListItem) : void {
			if (_doSelectedListItem) {
				if (_doSelectedListItem == doNewSelectionListItem) {
					return;
				} else {
					_doSelectedListItem.button.oController.setDeSelected();
				}
			}
			
			_doSelectedListItem = doNewSelectionListItem;
			_doSelectedListItem.button.oController.setSelected();
			
			if (_doSelectedListItem.item != stage) {
				dispatchEvent(new SelectionListEvent(SelectionListEvent.MARK_OBJECT, _doSelectedListItem.item));
			}
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// draw list
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function drawList() : void {

			var doStepper : DisplayObject;
			
			//:::::::
			// add up to stage
			doStepper = _doOriginalSelection.parent;
			
			while(doStepper != stage) {
				//if (!(doStepper is Stage)) {
					_a_doListElements[_a_doListElements.length] = doStepper;
				//}
				doStepper = doStepper.parent;
			}
			
			_a_doListElements.reverse();

			//if (_doOriginalSelection.parent is Stage) {

				for (var i : Number = 0;i < _doOriginalSelection.parent.numChildren;i++) {
					
					var doChild : DisplayObject = _doOriginalSelection.parent.getChildAt(i);
					_a_doListElements[_a_doListElements.length] = doChild;
					
					if (doChild == _doOriginalSelection && _doOriginalSelection is DisplayObjectContainer) {
						for (var j : Number = 0;j < DisplayObjectContainer(_doOriginalSelection).numChildren; j++) {
							_a_doListElements[_a_doListElements.length]=DisplayObjectContainer(_doOriginalSelection).getChildAt(j);
						}
					}
//				}
			}
			
			
			//:::::::
			// create  
			var intCountY : int = 0;
			
			for each (var item : DisplayObject in _a_doListElements) {
				
				var doSelectionListItem : SelectionListItem= new SelectionListItem(item);
				_sprPanelContent.addChild(doSelectionListItem);
				
				doSelectionListItem.y = intCountY * 14;
				doSelectionListItem.x = (DisplayChainTools.get_intNumStepsToStage(item) - 1) * 9+8;
				
				intCountY++;
				
				//:::::::
				// check if default seleciton list item 
				if (doSelectionListItem.item== _doOriginalSelection) {
					_doDefaultSelection = doSelectionListItem; 
				}
			}
			
			//:::::::
			// select default 
			markItem(_doDefaultSelection);
			
			//:::::::
			// place 
			var pBasePos : Point = _doDefaultSelection.parent.localToGlobal(new Point(_doDefaultSelection.parent.mouseX, _doDefaultSelection.parent.mouseY));
			
			x = pBasePos.x + 20;
			y = pBasePos.y - _doDefaultSelection.y - 5;
			
			//:::::::
			// draw back 
			drawPanelBack(new Point(_sprPanelContent.width, _sprPanelContent.height));
		}
	}
}
